package com.gyg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.gyg.domain.CloudMessage;
import com.gyg.dao.CloudMessageMapper;
import com.gyg.domain.CloudUser;
import com.gyg.returns.RespCode;
import com.gyg.returns.RespEntity;
import com.gyg.service.CloudMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyg.service.CloudUserService;
import com.gyg.utils.TimeUtilCustomize;
import com.gyg.vo.param.MessageParam;
import com.gyg.vo.result.MessageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 云盘消息表 服务实现类
 * </p>
 *
 * @author gyg
 * @since 2021-04-14
 */
@Service
public class CloudMessageServiceImpl extends ServiceImpl<CloudMessageMapper, CloudMessage> implements CloudMessageService {

    @Autowired
    private CloudUserService cloudUserService;

    @Override
    public RespEntity<List<MessageResult>> cloudMessageList() {

        QueryWrapper<CloudMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CloudMessage::getDeleted, 0)
                .orderByDesc(CloudMessage::getId);

        List<CloudMessage> cloudMessageList = baseMapper.selectList(queryWrapper);

        //公告数据组装
        List<MessageResult> messageResultList = Lists.newArrayList();
        for (CloudMessage cloudMessage : cloudMessageList) {
            //查询用户信息
            CloudUser cloudUser = cloudUserService.getById(cloudMessage.getUserId());

            MessageResult messageResult = MessageResult.builder()
                    .messageId(cloudMessage.getId())
                    .username(cloudUser.getName())
                    .picPath(cloudUser.getPicPath())
                    .messageContent(cloudMessage.getMessageContent())
                    .messageTime(cloudMessage.getMessageTime())
                    .messageUrl(cloudMessage.getMessageUrl())
                    .messageBan(cloudMessage.getMessageBan())
                    .build();
            messageResultList.add(messageResult);
        }



        return new RespEntity<>(RespCode.SUCCESS, messageResultList);
    }

    @Override
    public RespEntity insertMessage(MessageParam messageParam) {
        String messageTime = TimeUtilCustomize.parseDateToStr(TimeUtilCustomize.DATE_TIME_FORMAT, new Date());
        CloudMessage cloudMessage = CloudMessage.builder()
                .userId(messageParam.getUserId())
                .messageContent(messageParam.getContent())
                .messageUrl(messageParam.getUrl())
                .messageBan(0)
                .messageTime(messageTime)
                .build();
        baseMapper.insert(cloudMessage);

        return new RespEntity(RespCode.SUCCESS);
    }

}
